/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.serviet;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: javaweb - Tt
 * <p>Powered by webrx On 2022-03-15 17:08:33
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebServlet("/tt") @MultipartConfig
public class Tt extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println(req.getMethod());
        //System.out.println(req.getRemoteAddr());
        //System.out.println(req.getReader().readLine());

        System.out.println(req.getParameter("email"));
        System.out.println(req.getParameter("content"));
        Part file = req.getPart("file");

        String p = req.getServletContext().getRealPath("/upload/");
        File f = new File(p);
        if(!f.exists()) f.mkdirs();
        file.write(p+file.getSubmittedFileName());
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("msg","上传文件成功");
        out.print(JSON.toJSON(map));
        out.flush();
        out.close();

    }
}
